package com.sky.ok.okex.websocket.client;


import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.TimerTask;
import java.util.concurrent.*;

/**
 * @author 余心乐sky
 * @create 2019-02-01 8:42
 **/
public class test {

    public static void main(String[] args) throws InterruptedException
    {

        final TimerTask task1 = new TimerTask()
        {

            @Override
            public void run()
            {
                System.out.println("task1 invoked!");
                throw new RuntimeException();
            }
        };

        final TimerTask task2 = new TimerTask()
        {

            @Override
            public void run()
            {
                System.out.println("task2 invoked!");
            }
        };

      /*  ThreadFactory namedThreeadFactory = new ThreadFactoryBuilder().setNameFormat("websocket-pool-%d").build();
        //Commmon Thread Pool
        ExecutorService pool = new ThreadPoolExecutor(5,200,0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>(1024),namedThreeadFactory,new ThreadPoolExecutor.AbortPolicy());
        pool.execute(()-> System.out.println(Thread.currentThread().getName()));*/

        //pool.shutdown();


        /*ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.schedule(task1, 100, TimeUnit.MILLISECONDS);
        pool.scheduleAtFixedRate(task2, 0 , 2000, TimeUnit.MILLISECONDS);*/

        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
        //延迟首次执行的时间
        long initialDelay  =0L;
        //连续执行之间的时间间隔
        long period =3000L;

        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(System.currentTimeMillis());
                //do something
            }
        },initialDelay,period, TimeUnit.HOURS);

    }

}
